import { NTooltip, type TooltipProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";

export type TooltipFactoryConstructorType = {
  props?: TooltipProps;
  attrs?: HTMLAttributes;
  defaultSlot?: BaseComponentType;
  triggerSlot?: BaseComponentType;
  footerSlot?: BaseComponentType;
  headerSlot?: BaseComponentType;
};

export class TooltipFactory {
  private attrs: HTMLAttributes = {};
  private props: TooltipProps = {};
  private defaultSlot: BaseComponentType = null;
  private triggerSlot: BaseComponentType = null;
  private footerSlot: BaseComponentType = null;
  private headerSlot: BaseComponentType = null;
  constructor(param?: TooltipFactoryConstructorType) {
    if (param?.props) this.setProps(param.props);
    if (param?.attrs) this.setAttrs(param.attrs);
    if (param?.defaultSlot) this.setDefault(param.defaultSlot);
    if (param?.triggerSlot) this.setTrigger(param.triggerSlot);
    if (param?.footerSlot) this.setFooter(param.footerSlot);
    if (param?.headerSlot) this.setHeader(param.headerSlot);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }
  setProps(props: TooltipProps) {
    this.props = props;
    return this;
  }
  setDefault(defaultSlot: BaseComponentType) {
    this.defaultSlot = defaultSlot;
    return this;
  }
  setTrigger(triggerSlot: BaseComponentType) {
    this.triggerSlot = triggerSlot;
    return this;
  }
  setFooter(footerSlot: BaseComponentType) {
    this.footerSlot = footerSlot;
    return this;
  }
  setHeader(headerSlot: BaseComponentType) {
    this.headerSlot = headerSlot;
    return this;
  }

  create() {
    return (
      <NTooltip {...this.attrs} {...this.props}>
        {{
          default:
            this.defaultSlot != null
              ? () => getSlotsDom(this.defaultSlot)
              : null,

          trigger:
            this.triggerSlot != null
              ? () => getSlotsDom(this.triggerSlot)
              : null,
          footer:
            this.footerSlot != null ? () => getSlotsDom(this.footerSlot) : null,
          header:
            this.headerSlot != null ? () => getSlotsDom(this.headerSlot) : null,
        }}
      </NTooltip>
    );
  }
}
